var ajax2=new sack();

function initMenu() {
	
$('#menu ul').hide();
$('#menu ul:first').show();
$('#menu li a').click(
function() {
  var checkElement = $(this).next();
  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
	return false;
	}
  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
	$('#menu ul:visible').slideUp('normal');
	checkElement.slideDown('normal');
	return false;
	}
  }
);
}
function initMenu1() 
{
$('#menu1 ul').hide();
$('#menu1 ul:first').show();
$('#menu1 li a').click(
function() {
  var checkElement = $(this).next();
  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
	return false;
	}
  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
	$('#menu1 ul:visible').slideUp('normal');
	checkElement.slideDown('normal');
	return false;
	}
  }
);
}
var bearing = 0, timeout = null;
function rotate(newBearing, dir) {
//	alert(newBearing+" "+dir)
$('#binnacle')
	  .css({'-webkit-transform': 'rotate(' + newBearing + 'deg)'})
	  .css({'-moz-transform': 'rotate(' + newBearing + 'deg)'})
	  ;
  /*clearTimeout(timeout);
  timeout = setTimeout(function() {
	bearing += dir;
	if (bearing > 360) bearing -= 360;
	if (bearing < 0) bearing += 360;
	$('#binnacle')
	  .css({'-webkit-transform': 'rotate(' + bearing + 'deg)'})
	  .css({'-moz-transform': 'rotate(' + bearing + 'deg)'})
	  ;
	if ((bearing % 360) === newBearing) return;
	timeout = setTimeout(arguments.callee, 50);
  }, 50);*/
};
var interVal;
for(j=1; j <= 10; j++)
{
	if(j == 1)
	interVal +='<option value='+j+' selected=selected >'+j+'</option>';
	else
	interVal +='<option value='+j+'>'+j+'</option>';
}

var routeSelect;
var allRoute;
var refreshSelect;
var messages;
var map;
var intervalID;
var newInterval = 10;
var currentInterval;
var zoomLevelSelect;
var zoomLevel;
var autoRefresh = 1;
var mapPoints;
var hsp;
var interval;
var date_offline;
var mapheight;

function loadMap(date_offline,sessionid,ssId,vehiNo,rFlag,frmTime,toTime) 
{
	/*resolution = screen.width+' x '+screen.height
    alert('Your resolution is: ' + resolution);

    if (resolution == "1024 x 768")
    {
		document.getElementById('tab1').style.height = "300px;"
    }
    else
    {
        document.getElementById('tab1').style.height = "400px;"
    }*/
	
	
	//date_offline = "<?php echo date("d-m-Y");?>";
	routeSelect = document.getElementById('selectRoute');
	refreshSelect = document.getElementById('selectRefresh');
	zoomLevelSelect = document.getElementById('selectZoomLevel');
	messages = document.getElementById('messages');
	map = document.getElementById("map_canvas");
	geocoder = new GClientGeocoder();

	intervalID = 0;
	newInterval = 30;
	currentInterval = 0;
	zoomLevel = 16;
	//mapType=3;
	mapPoints=5;
	hsp=0;
	interval=1;
	
	$('#speedMeter').speedometer(0);

	if(date_offline!='')
	{
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1
		var day = currentTime.getDate()
		var year = currentTime.getFullYear()
		to_day = year + "-" + month + "-" + day;
		
		var check_date1 = to_day.split('-');
		var now_date1 = new Date(check_date1[0],check_date1[1]-1,check_date1[2]);
		
		var check_date2 = date_offline.split('-');
		var now_date2 = new Date(check_date2[0],check_date2[1]-1,check_date2[2]);
		
		var diff_date = now_date2 - now_date1;
		if(diff_date == 0)
		{
			for_Poly = 0;
		}
		else
		{
			for_Poly = 1;
		}
	}
	if(date_offline != '' || sessionid != '')
	{
		//mapheight = $(window).height() - 172; //enter hight of header and footers here
		//$("#map_canvas").height(mapheight);
		//$("#reportView").height(mapheight);
		//$('#speedMeter').speedometer({ percentage: 0 });
		//$("#mapTable").height(mapheight);
		hasMap();
		if(vehiNo)
		{
			dt = date_offline.split("-");
			myclick(ssId,dt[0],dt[1],dt[2],vehiNo,rFlag) 
			document.getElementById('time3').value = frmTime;
			document.getElementById('time4').value = toTime;
			showMapOnDate(date_offline,frmTime,date_offline,toTime,vehiNo) ;
		}
		else
		{
			getAllVehicle(date_offline,sessionid);
		}
	}
	else
	{
		showWait('No Data');
		map.innerHTML = '<img src="images/ajax-loader.gif" />';
	}
	//zoomLevelSelect.selectedIndex = 14;
	//showWait('Loading Routes...');
}

//var refreshId = setInterval("refreshMapTable("+1+","+<?php echo $date_offline;?>+")", 1000);

function refreshMapTable(divid,sessionid,date_offline)
{
	ajax2.requestFile = 'ajax_server.php?date_offline='+date_offline+'&sessionid='+sessionid;
	//alert(ajax2.requestFile)
 	if(divid!='')
	document.getElementById(divid).innerHTML = "Refreshing...";
	ajax2.onCompletion = function(){exeRefreshTable()};
	ajax2.runAJAX();
}
function exeRefreshTable()
{
	if(ajax2.response != "")
	{
		document.getElementById('mapTable').innerHTML = ajax2.response;
		initMenu();
		initMenu1();
		
	}
	else
	{
		document.getElementById('mapTable').innerHTML = 'No data found';
	}
}

function exeReportTable(rtData)
{
	var result = rtData.split("@");
	//document.write(result);
	var mapTable = '<table id="reportform_final" border="0" cellpadding="3" cellspacing="2">';
    mapTable +='<tr><th width="5%">#</th><th width="7%">Date</th><th width="7%">Time</th><th width="20%">Dev. Name</th><th width="20%">IMEI</th><th width="14%">Location</th><th width="7%">Altitude</th><th width="7%">Speed</th><th width="7%">Direction</th></tr>';
	
	no_of_rows= result.length-1;
	if(no_of_rows > 0)
	{
		for(i=0;i<no_of_rows;i++)
		{
			repData = result[i].split(',');
			//alert(data[2].split(" "));
			date1 = repData[4].split(" ");

			mapTable +='<tr>';
			mapTable +='<td valign="top"><input type="radio" id="btnGoOpt" name="btnGoOpt" onclick="funnGoOpt('+i+')" />'+(i+1)+'</td>';
			mapTable +='<td valign="top">'+date1[0]+'</td>';
			mapTable +='<td valign="top">'+date1[1]+'</td>';
			mapTable +='<td valign="top">'+repData[5]+'</td>';
			mapTable +='<td valign="top">'+repData[6]+'</td>';
			mapTable +='<td valign="top"><a href=http://maps.google.com/maps?q='+repData[12]+','+repData[13]+' target=_blank title=click here>'+repData[12]+','+repData[13]+'</a></td>';
			//mapTable +='<td valign="top">f'+repData[13]+'</td>';
			mapTable +='<td valign="top">'+repData[11]+'</td>';
			mapTable +='<td valign="top">'+repData[1]+'</td>';
			mapTable +='<td valign="top">'+repData[2]+'</td>';
			mapTable +='</td></tr>';
			
		}//end of for loop
	}//end of if loop
	else
	{
		mapTable +='<tr><td colspan="3" style="border-top:1px solid #c5d4da; border-right:0px; background-color:#e8e9ea;">No Records found</td></tr>';
	}
	mapTable +='</table>';
	document.getElementById('reportView').style.height = mapheight+"px";
	document.getElementById('reportView').style.overflowY = "scroll";
	document.getElementById('reportView').innerHTML = mapTable;
	document.getElementById('devSpeedMeter').style.display = 'none';

}

function pickThisDevice(devId)
{
	if(devId!=0)
	{
		document.getElementById('hidTxtDevId').value = devId;
		document.getElementById('cmdFindData').disabled = false;
		document.getElementById('cmdFindData').className = 'btnFindACSS';
		document.getElementById('selectRefresh').disabled = false;
		//document.frmMapData.chkShowBubble.disabled = true;
		
	}
	else
	{
		document.getElementById('cmdFindData').className = 'btnFindNACSS';
		document.getElementById('cmdFindData').disabled = true;
	}
}
function funnGoOpt(opt)
{
	$("ul.tabs li").removeClass("active");
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	
	//alert("sss"+activeTab+" "+opt);
	/* //Remove any "active" class
	$("#tab1").addClass("active"); //Add "active" class to selected tab
	$(".tab_content").hide(); //Hide all tab content
	$("#tab1").fadeIn(); //Fade in the active content*/
	//alert(playMarkId.length+" "+playMarkId[opt])
	rdcot = playMarkId[opt];
	createPlayMarker(rdcot,map,myMarker,myMarker.length);
	//GEvent.trigger(myGMarker[opt], "click");
}